<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

/**
 * 
 */
class Computer extends Common
{
	public function before_index($bucket='',$prefix='',$delimiter='/')
	{
		$prefix = !empty($_GET['prefix'])?$_GET['prefix']:'';
		$oss_result = $this->_getAllFolderList($this->getOssBucketList($bucket,$prefix,$delimiter));
		$this->assign(['fileList' => $oss_result,'prefix'=>$prefix]);
	}

	public function inlist($bucket='',$prefix='',$delimiter='/'){
		$prefix = !empty($_GET['prefix'])?$_GET['prefix']:'';
		$oss_result = $this->_getOneFolderList($this->getOssBucketList($bucket,$prefix,$delimiter));
		//print_r($oss_result );exit;
		$this->assign(['fileList' => $oss_result]);
		return view('list',['fileList' => $oss_result,'prefix'=>$prefix]);
	}

	public function _getOneFolderList($object)
	{
		$nextMarker = $object->getNextMarker();
		$bucketName = $object->getbucketName();
		$prefix = $object->getprefix();
		$fileList = array();
		if($listObject = $object->getObjectList()){
			foreach($listObject as $info){
				$name =  $info->getKey();
				$size = $info->getsize();
				$lastModified = $info->getLastModified();
				$url = 'http://oo.oopp.top/'.$name;
				$fileList[] = array(
					'url'  => $url,
                    'name' => $name,
					'type' => !empty(_JudgementFile($url,$name)['name'])?_JudgementFile($url,$name)['name']:'其他',
					'thumb'=> !empty(_JudgementFile($url,$name)['thumb'])?_JudgementFile($url,$name)['thumb']:dirname($_SERVER['SCRIPT_NAME']).'/static/admin/images/f01.png',
					'size' => $size,
                    'time' => $lastModified
                );
        	}
		}
		return $fileList;
	}
	public function _getAllFolderList($object)
	{
		$nextMarker = $object->getNextMarker();
		$fileList = array();
		$storage = 0;
		$num = 1;
		if($listObject = $object->getObjectList()){
			foreach($listObject as $info){
				$name =  $info->getKey();
				$size = $info->getsize();
				$lastModified = $info->getLastModified();
				$fileList['num'] = $num++;
				$fileList['storage'] = $storage+=$size;
				$fileList['mainmenu'][] = array(
                    'name' => $name,
					'size' => $size,
                    'lastModified' => $lastModified
                );
        	}
		}
		if($listPrefix = $object->getPrefixList()){
			foreach($listPrefix as $info){
				$folder = $info->getPrefix();
				$fileList['submenu'][] = array(
                    'name' => $folder,
					'object' => $this->_getAllFolderList($this->getOssBucketList('',$folder))
                );
        	}
		}
		return $fileList;
	}
	public function before_add()
	{
		if(!empty($_GET['object']) && $_GET['object']){
			 session('DIR_FOLDER',$_GET['object']);
		}else{
			 session('DIR_FOLDER',null);	
		}

	}
	public function before_edit($value='')
	{
		// echo "我是修改前缀操作！";
	}

}
